package com.tgy.binarysearch;

import java.util.Objects;

/**
 * @Author: tgy
 * @Date: 2020-11-17 17:32
 *
 * 二分查找返回刚好大于value的元素下标，如无return -1
 */
public class BSFirstLGValue {

    public int binarySearch(Integer[] nums, int value) {


        if (Objects.isNull(nums) || nums.length == 0) {

            return -1;
        }

        if (nums[0] > value) {

            return 0;
        }

        if (nums[nums.length - 1] <= value) {

            return -1;
        }

        int left = 0,right = nums.length,mid;

        while (left + 1 < right) {

            mid = (left + right) >> 1;

            if (value >= nums[mid]) {

                left = mid;
            }else {

                right = mid;
            }
        }

        return right;
    }
}
